package com.base.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.base.bean.AbsHibernateBean;
/**
 * 抽象dao 公共方法实现
 * @author Lin Wang
 * 
 * @param <ID>
 * @param <T>
 */

public abstract class AbsTaskBaseDao<ID extends Serializable,T extends AbsHibernateBean> extends HibernateDaoSupport implements IBaseDao<ID, T>{
	final private Logger logger = LoggerFactory.getLogger(AbsTaskBaseDao.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public ID save(T entity) 
	{
		if (entity == null) {
			logger.error("新增的实体不能为null");
			return null;
		}
		return (ID) this.getHibernateTemplate().save(entity);
	}
	
	@Override
	public void saveOrUpdate(T entity) 
	{
		if (entity == null) {
			logger.error("新增或更新的实体不能为null");
			return;
		}
		this.getHibernateTemplate().saveOrUpdate(entity);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> listByHQL(String hql, int from, int count) 
	{
		if(hql == null || hql.trim().length() == 0){
			return null;
		}
		Session session = null;
		try {
			session = this.getSession();
			Query query = session.createQuery(hql);
			if (count >= 0) {
				query.setFirstResult(from);
				query.setMaxResults(count);
			}
			return query.list();
		} catch (Exception e) {
			logger.error("查询异常：",e);
		} finally {
			this.releaseSession(session);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> listByHQL(String hql) 
	{
		if(hql == null || hql.trim().length() == 0){
			return null;
		}
		Session session = null;
		try {
			session = this.getSession();
			Query query = session.createQuery(hql);
			return query.list();
		} catch (Exception e ) {
			logger.error("查询异常：", e);
		} finally {
			this.releaseSession(session);
		}
		return null;
	}
	
	@Override
	public long getCount(String hql) 
	{
		if(hql == null || hql.trim().length() == 0){
			return 0;
		}
		
		Session session = null;
		try {
			session = this.getSession();
			Query query = session.createQuery(hql);
			
			return query.list().size();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询异常：",  e);
		} finally {
			this.releaseSession(session);
		}
		return 0;
	}

	@Override
	public void del(T entity) 
	{
		if (entity == null) {
			logger.error("删除的实体不能为null");
			return ;
		}
		this.getHibernateTemplate().delete(entity);
	}

	@Override
	public int del(String hql) 
	{
		if(hql == null || hql.trim().isEmpty()) {
			logger.error("hql is null");
			return -1;
		}
		
		Session session = null;
		try {
			session = this.getSession();
			Query query = session.createQuery(hql);
			return query.executeUpdate();
		} catch (Exception e) {
			logger.error("删除失败：", e);
		} finally {
			this.releaseSession(session);
		}
		return 0;
	}

	@Override
	public int addBatch(List<T> entities) 
	{
		if(entities == null || entities.isEmpty()) {
			logger.error("增加集合不能为空。");
			return 0;
		}
		int count = 0;
		Session session = null;
		try {
			session = this.getSession();
			session.beginTransaction();
			
			for (T entity : entities) {
				count ++;
				session.save(entity);
			}

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error("批量增加出错：", e);
			session.getTransaction().rollback();
		} finally {
			this.releaseSession(session);
		}
		return count;
	}

	@Override
	public int delBatch(List<T> entities) 
	{
		if(entities == null || entities.isEmpty()) {
			logger.error("删除集合不能为空。");
			return 0;
		}
		int count = 0;
		Session session = null;
		try {
			session = this.getSession();
			session.beginTransaction();
			
			for (T entity : entities) {
				count ++;
				session.delete(entity);
			}

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error("批量删除出错：", e);
			session.getTransaction().rollback();
		} finally {
			this.releaseSession(session);
		}
		return count;
	}
	
	/**
	 * 批量更新
	 * @param entities
	 * @return
	 */
	@Override
	public int updateBatch(List<T> entities) 
	{
		if(entities == null || entities.isEmpty()) {
			logger.error("修改集合不能为空。");
			return 0;
		}
		int count = 0;
		Session session = null;
		try {
			session = this.getSession();
			session.beginTransaction();
			
			for (T entity : entities) {
				count ++;
				session.update(entity);
			}

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error("批量修改出错：", e);
			session.getTransaction().rollback();
		} finally {
			this.releaseSession(session);
		}
		return count;
	}
}
